//
//  JSTMyDynamicCell.m
//  KYKJPackDemo
//
//  Created by 01 on 17/5/26.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTMyDynamicCell.h"
#import "JSTInterestcircleDataModel.h"
#import "MLEmojiLabel.h"

@interface JSTMyDynamicCell ()<MLEmojiLabelDelegate>

@property (nonatomic, strong) UIImageView           *iconView;

@property (nonatomic, strong) UILabel               *nickLabel;

@property (nonatomic, strong) UILabel               *timeLabel;

@property (nonatomic, strong) UIButton              *deleteBtn;

@property (nonatomic, strong) MLEmojiLabel          *contentLabel;

@property (nonatomic, strong) UIView                *imgContentView;

@property (nonatomic, strong) UIView                *lineView;

@property (nonatomic, strong) UIButton              *shareBtn;

@property (nonatomic, strong) UIButton              *commentBtn;

@property (nonatomic, strong) UIButton              *fabulousBtn;

@property (nonatomic, strong) NSMutableArray        *imgData;

@property (nonatomic, strong) UILabel               *noDataLabel;

@property (nonatomic, strong) JSTInterestcircleDataModel *model;

@end

@implementation JSTMyDynamicCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _iconView = [[UIImageView alloc] init];
        [self addSubview:_iconView];
        
        _nickLabel = [[UILabel alloc] init];
        [self addSubview:_nickLabel];
        
        _timeLabel = [[UILabel alloc] init];
        [self addSubview:_timeLabel];
        
        _deleteBtn = [[UIButton alloc] init];
        [self addSubview:_deleteBtn];
        
        _contentLabel = [[MLEmojiLabel alloc] init];
        [self addSubview:_contentLabel];
        
        _imgContentView = [[UIView alloc] init];
        [self addSubview:_imgContentView];
        
        _lineView = [[UIView alloc] init];
        [self addSubview:_lineView];
        
        _shareBtn = [[UIButton alloc] init];
        [self addSubview:_shareBtn];
        
        _commentBtn = [[UIButton alloc] init];
        [self addSubview:_commentBtn];
        
        _fabulousBtn =[[UIButton alloc] init];
        [self addSubview:_fabulousBtn];
        
        _noDataLabel = [[UILabel alloc] init];
        [self addSubview:_noDataLabel];
    }
    return self;
}

/**
 *  根据模型创建子控件设置frame
 */
- (void)createContent:(JSTInterestcircleDataModel *)model
{
    if (!model) {
        CGFloat noDataX = 0;
        CGFloat noDataY = 0;
        CGFloat noDataW = kScreenWidth;
        CGFloat noDataH = kCurrentScreenWidth(100);
        _noDataLabel.frame = CGRectMake(noDataX, noDataY, noDataW, noDataH);
        _noDataLabel.text = @"暂无动态";
        _noDataLabel.font = kSystemFont(kCurrentScreenWidth(14.0f));
        _noDataLabel.textAlignment = NSTextAlignmentCenter;
        
        return;
    }
    _model = model;
    
    CGFloat iconX = kCurrentScreenWidth(10);
    CGFloat iconY = kCurrentScreenWidth(10);
    CGFloat iconW = kCurrentScreenWidth(30);
    CGFloat iconH = kCurrentScreenWidth(30);
    _iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    [_iconView setImageWithURL:[NSURL URLWithString:model.iconImgUrl]];
    
    CGFloat nickX = CGRectGetMaxX(_iconView.frame) + kCurrentScreenWidth(10);
    CGFloat nickY = iconY;
    CGSize nickSize = [NSString sizeWithString:model.userName font:kSystemFont(kCurrentScreenWidth(12)) CGsize:CGSizeMake(kScreenWidth/2, MAXFLOAT)];
    _nickLabel.frame = (CGRect){{nickX, nickY}, nickSize};
    _nickLabel.text = model.userName;
    _nickLabel.font = kSystemFont(kCurrentScreenWidth(12));
    
    CGFloat timeX = nickX;
    CGSize timeSize = [NSString sizeWithString:model.timeText font:kSystemFont(kCurrentScreenWidth(10)) CGsize:CGSizeMake(kScreenWidth/2, MAXFLOAT)];
    CGFloat timeY = CGRectGetMaxY(_iconView.frame) - timeSize.height;
    _timeLabel.frame = (CGRect){{timeX, timeY}, timeSize};
    _timeLabel.text = model.timeText;
    _timeLabel.font = kSystemFont(kCurrentScreenWidth(10));
    
    CGSize deleteSize = [NSString sizeWithString:@"删除" font:kSystemFont(kCurrentScreenWidth(11)) CGsize:CGSizeMake(kScreenWidth, MAXFLOAT)];
    CGFloat deleteX = kScreenWidth - 2*iconX - deleteSize.width;
    CGFloat deleteY = 0;
    CGFloat deleteW = deleteSize.width + iconX*2;
    CGFloat deleteH = deleteSize.height + iconY*2;
    _deleteBtn.frame = CGRectMake(deleteX, deleteY, deleteW, deleteH);
    [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    _deleteBtn.titleLabel.font = kSystemFont(kCurrentScreenWidth(11));
    [_deleteBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_deleteBtn addTarget:self action:@selector(deleteCurrentDynamic) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat contentX = iconX;
    CGFloat contentY = CGRectGetMaxY(_iconView.frame) + kCurrentScreenWidth(10);
    CGSize contentSize = [NSString sizeWithString:model.contentText font:kSystemFont(kCurrentScreenWidth(10)) CGsize:CGSizeMake(kScreenWidth - 2*contentX, MAXFLOAT)];
    
    _contentLabel.font = kSystemFont(kCurrentScreenWidth(10));
    _contentLabel.numberOfLines = 0;
    _contentLabel.textInsets = UIEdgeInsetsMake(5, 0, 0, 0);
    _contentLabel.textColor= [UIColor colorWithHexString:@"595757"];
    _contentLabel.emojiDelegate = self;
    _contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    _contentLabel.isNeedAtAndPoundSign = YES;
    _contentLabel.disableThreeCommon=YES;
    // _contentLabel.customEmojiRegex =@"[\\[e_]+[0-9]{1,4}+[\\]]";
    _contentLabel.customEmojiRegex = @"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]";
    _contentLabel.customEmojiPlistName =@"Emoji_icon.plist";
    [_contentLabel setEmojiText:model.contentText];
    _contentLabel.frame = (CGRect){{contentX, contentY}, contentSize};
    [_contentLabel sizeToFit];
    
    CGFloat lineY;
    if (IsArrEmpty(model.contentImgUrlArr)) {
        [_imgContentView removeFromSuperview];
        
        lineY = CGRectGetMaxY(_contentLabel.frame) + kCurrentScreenWidth(10);
    } else {
        CGFloat imgX = 0;
        CGFloat imgY = CGRectGetMaxY(_contentLabel.frame) + kCurrentScreenWidth(10);
        CGFloat imgW = kScreenWidth;
        CGFloat imgH = (kScreenWidth - iconX * 4) / 3;
        _imgContentView.frame = CGRectMake(imgX, imgY, imgW, imgH);
        [self createImageContent:model.contentImgUrlArr];

        lineY = CGRectGetMaxY(_imgContentView.frame) + kCurrentScreenWidth(10);
    }
    
    CGFloat lineX = 0;
    CGFloat lineW = kScreenWidth;
    CGFloat lineH = 0.35;
    _lineView.frame = CGRectMake(lineX, lineY, lineW, lineH);
    _lineView.backgroundColor = [UIColor grayColor];
    
    CGFloat shareX = 0;
    CGFloat shareY = CGRectGetMaxY(_lineView.frame);
    CGFloat shareW = iconX * 2 + kCurrentScreenWidth(24);
    CGFloat shareH = kCurrentScreenWidth(35);
    _shareBtn.frame = CGRectMake(shareX, shareY, shareW, shareH);
    [_shareBtn setImage:[UIImage imageNamed:@"Mydynamic_share_normal"] forState:UIControlStateNormal];
    [_shareBtn addTarget:self action:@selector(shareCurrentComment) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat commentX = kScreenWidth / 2;
    CGFloat commentY = CGRectGetMaxY(_lineView.frame);
    CGFloat commentW = (kScreenWidth - 2 * iconX) / 4;
    CGFloat commentH = kCurrentScreenWidth(35);
    _commentBtn.frame = CGRectMake(commentX, commentY, commentW, commentH);
    [_commentBtn setImage:[UIImage imageNamed:@"Mydynamic_comments_normal"] forState:UIControlStateNormal];
    [_commentBtn setTitle:model.commentNumber forState:UIControlStateNormal];
    [_commentBtn setTitleColor:[UIColor colorWithHexString:@"141414"] forState:UIControlStateNormal];
    [_commentBtn addTarget:self action:@selector(commentCurrent) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat fabulousX = CGRectGetMaxX(_commentBtn.frame);
    CGFloat fabulousY = commentY;
    CGFloat fabulousW = commentW;
    CGFloat fabulousH = _commentBtn.height;
    _fabulousBtn.frame = CGRectMake(fabulousX, fabulousY, fabulousW, fabulousH);
    [_fabulousBtn setImage:[UIImage imageNamed:@"Mydynamic_Thumbup_normal"] forState:UIControlStateNormal];
    [_fabulousBtn setImage:[UIImage imageNamed:@"Mydynamic_Thumbup_hover"] forState:UIControlStateSelected];
    [_fabulousBtn setTitle:model.pointPraise forState:UIControlStateNormal];
    [_fabulousBtn setTitleColor:[UIColor colorWithHexString:@"141414"] forState:UIControlStateNormal];
    [_fabulousBtn addTarget:self action:@selector(fabulousCurrentComment) forControlEvents:UIControlEventTouchUpInside];
    
    _cellHeight = CGRectGetMaxY(_fabulousBtn.frame);
}

/**
 *  根据图片数组设置图片
 */
- (void)createImageContent:(NSArray *)imgData
{
    [imgData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         UIImageView *imgView = [[UIImageView alloc] init];
         imgView.tag = 100 + idx;
         CGFloat imageX = kCurrentScreenWidth(10) + ((kScreenWidth - kCurrentScreenWidth(10))/3)*idx;
         CGFloat imageY = 0;
         CGFloat imageW = ((kScreenWidth - kCurrentScreenWidth(10))/3.) - kCurrentScreenWidth(10);
         CGFloat imageH = imageW;
         imgView.frame = CGRectMake(imageX, imageY, imageW, imageH);
         imgView.userInteractionEnabled = YES;
         //描边
         imgView.layer.borderWidth = 0.5;
         imgView.layer.borderColor = [UIColor colorWithHexString:@"e0e0e0"].CGColor;
         //添加手势
         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
         [imgView addGestureRecognizer:tap];
         
         [imgView setImageWithURL:[NSURL URLWithString:obj]];
         [_imgContentView addSubview:imgView];
     }];
}

#pragma mark - method
- (void)commentCurrent
{
    if (_delegate && [_delegate respondsToSelector:@selector(changeCommentController:)]) {
        [_delegate changeCommentController:_model.dynamicId];
    }
}

- (void)shareCurrentComment
{
    NSMutableArray *shareData = [NSMutableArray array];
    
    [shareData addObject:_model.contentText];
    
    UIImage *image;
    if (!IsArrEmpty(_model.contentImgUrlArr))
    {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[_model.contentImgUrlArr safeObjectAtIndex:0]]];
        image = [UIImage imageWithData:data];
        [shareData addObject:image];
    }
    
//    [_model.contentImgUrlArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:obj]];
//        UIImage *image = [UIImage imageWithData:data];
//        [shareData addObject:image];
//    }];
    
    if (_delegate && [_delegate respondsToSelector:@selector(shareCurrentComment:)])
    {
        [_delegate shareCurrentComment:shareData];
    }
}

- (void)fabulousCurrentComment
{
    if (_delegate && [_delegate respondsToSelector:@selector(pointPraiseForCurrentComment:)])
    {
        [_delegate pointPraiseForCurrentComment:_model.dynamicId];
    }
}

- (void)deleteCurrentDynamic
{
    if (_delegate && [_delegate respondsToSelector:@selector(deleteCurrentDynamic:)])
    {
        [_delegate deleteCurrentDynamic:_model.dynamicId];
    }
}

#pragma mark 点击图片放大
- (void)tapClick:(UITapGestureRecognizer *)tap
{
    UIImageView *tapImageView = (UIImageView *)tap.view;
    
    [JSTScanImage scanBigImageWithImageView:tapImageView];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
